package operation.duoxianliu.demo02;

public class liping implements Runnable{
    private static int saeaf=100;
    @Override
    public void run() {
        while (true) {
            if ("小红".equals(Thread.currentThread().getName())) {
                boolean asdzcawd = asdzcawd();
                if (asdzcawd) {
                    break;
                }
            }
            if ("小明".equals(Thread.currentThread().getName()))
                synchronized (liping.class) {
                    if (saeaf <= 10) {
                        break;
                    } try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    saeaf--;
                        System.out.println(Thread.currentThread().getName() + "还剩" + saeaf + "份礼品");

                }
        }
    }
    private static synchronized boolean asdzcawd() {
        if(saeaf<=10){
            return true;
        }else {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            saeaf--;
            System.out.println(Thread.currentThread().getName()+"还剩"+saeaf+"份");
        }
        return false;
    }
}
